package com.lemon.smartsafetynew.ui.adapter.patrol;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolMediaVo;

import java.util.List;

import cn.finalteam.galleryfinal.widget.GFImageView;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/8/28.
 */

public class PatrolAttachmentAdapter extends LemonRcvAdapter<SafePatrolMediaVo> {

    private RecyclerTClickListener<SafePatrolMediaVo> mListener;

    public PatrolAttachmentAdapter(@Nullable List<SafePatrolMediaVo> data,
                                   RecyclerTClickListener<SafePatrolMediaVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new MediaItem();
    }

    private class MediaItem implements AdapterItem<SafePatrolMediaVo> {

        private LinearLayout ripple;
        private GFImageView ivPhoto;
        private ImageView ivDelete;


        private int mPosition;
        private SafePatrolMediaVo mData;

        @Override
        public int getLayoutResId() {
            return R.layout.item_evidence_photo;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            ivPhoto = (GFImageView) root.findViewById(R.id.ivPhoto);
            ivDelete = (ImageView) root.findViewById(R.id.ivDelete);
            ivDelete.setVisibility(View.GONE);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(SafePatrolMediaVo safePatrolMediaVo, int position) {
            mData = safePatrolMediaVo;
            mPosition = position;
            try {
                if (LemonFileHelper.isVoiceUrl(safePatrolMediaVo.getData_url())) {
                    ivPhoto.setImageDrawable(ContextCompat.getDrawable(IApplication.context(), R.mipmap.icon_new_voice));
                } else {
                    if (StringUtils.nullValue(safePatrolMediaVo.getData_url()).length() != 0) {
                        //Log.e("yxy3", "HttpUtils.formatFilesUrl(mData.getData_url()) = " + HttpUtils.formatFilesUrl(mData.getData_url()));
                        GlideImageLoader.loadImage(ivPhoto.getContext(), HttpUtils.formatFilesUrl(mData.getData_url()), ivPhoto);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
